package com.grade.system.service.impl;

import java.util.List;

import com.grade.common.core.domain.entity.SysUser;
import com.grade.common.exception.ServiceException;
import com.grade.common.utils.DateUtils;
import com.grade.common.utils.StringUtils;
import com.grade.common.utils.security.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.grade.system.mapper.GradeImportMapper;
import com.grade.system.domain.GradeImport;
import com.grade.system.service.IGradeImportService;
import com.grade.common.core.text.Convert;

/**
 * 已发布信用分导入Service业务层处理
 * 
 * @author grade
 * @date 2021-10-30
 */
@Service
public class GradeImportServiceImpl implements IGradeImportService 
{
    @Autowired
    private GradeImportMapper gradeImportMapper;

    /**
     * 查询已发布信用分导入
     * 
     * @param Id 已发布信用分导入主键
     * @return 已发布信用分导入
     */
    @Override
    public GradeImport selectGradeImportById(Long Id)
    {
        return gradeImportMapper.selectGradeImportById(Id);
    }

    /**
     * 查询已发布信用分导入列表
     * 
     * @param gradeImport 已发布信用分导入
     * @return 已发布信用分导入
     */
    @Override
    public List<GradeImport> selectGradeImportList(GradeImport gradeImport)
    {
        return gradeImportMapper.selectGradeImportList(gradeImport);
    }

    /**
     * 新增已发布信用分导入
     * 
     * @param gradeImport 已发布信用分导入
     * @return 结果
     */
    @Override
    public int insertGradeImport(GradeImport gradeImport)
    {
        gradeImport.setCreateTime(DateUtils.getNowDate());
        return gradeImportMapper.insertGradeImport(gradeImport);
    }

    /**
     * 修改已发布信用分导入
     * 
     * @param gradeImport 已发布信用分导入
     * @return 结果
     */
    @Override
    public int updateGradeImport(GradeImport gradeImport)
    {
        gradeImport.setUpdateTime(DateUtils.getNowDate());
        return gradeImportMapper.updateGradeImport(gradeImport);
    }

    /**
     * 批量删除已发布信用分导入
     * 
     * @param Ids 需要删除的已发布信用分导入主键
     * @return 结果
     */
    @Override
    public int deleteGradeImportByIds(String Ids)
    {
        return gradeImportMapper.deleteGradeImportByIds(Convert.toStrArray(Ids));
    }

    /**
     * 批批量删除已发布信用分导入(删除某个地区的某个考核周期内的全部排名数据)
     *
     * @param Ids 需要删除的已发布信用分导入主键
     * @return 结果
     */
    @Override
    public int deleteBatchGradeImportByIds(String Ids)
    {
        String[] ids=Convert.toStrArray(Ids);
        for (int i=0;i<ids.length;i++){
            GradeImport gradeImport=gradeImportMapper.selectGradeImportById(Long.parseLong(ids[i]));
            if (gradeImport!=null){
                gradeImportMapper.deleteBatchGradeImport(gradeImport);
            }
        }
        return 1;
    }

    /**
     * 删除已发布信用分导入信息
     * 
     * @param Id 已发布信用分导入主键
     * @return 结果
     */
    @Override
    public int deleteGradeImportById(Long Id)
    {
        return gradeImportMapper.deleteGradeImportById(Id);
    }

    @Override
    public String importGrade(List<GradeImport> gradeImportList, boolean updateSupport, String loginName) {
        if (StringUtils.isNull(gradeImportList) || gradeImportList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (GradeImport gradeImport:  gradeImportList)
        {
            try
            {
                //判断是否存在排名数据，若存在则更新
                List<GradeImport> gradeImportList1=gradeImportMapper.selectGradeImportByCondition(gradeImport);
                if (gradeImportList1.size()==1){
                    gradeImport.setId(gradeImportList1.get(0).getId());
                    gradeImport.setUpdateTime(DateUtils.getNowDate());
                    gradeImportMapper.updateGradeImport(gradeImport);
                }else {
                    gradeImport.setCreateTime(DateUtils.getNowDate());
                    gradeImportMapper.insertGradeImport(gradeImport);
                }
                successNum++;
                successMsg.append("<br/>" + successNum + "、地区 " +gradeImport.getCity()+ " 评价标准名称 "
                        +gradeImport.getGradeName() + " 考核周期 " + gradeImport.getGradePeriod());
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、地区 " +gradeImport.getCity()+ " 评价标准名称 "
                        +gradeImport.getGradeName() + " 考核周期 " + gradeImport.getGradePeriod() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
